﻿using System.Diagnostics;
using System.Windows;
using System.Windows.Input;
using WpfApp1.Models;

namespace WpfApp1.Controls
{
    public static class DragAndDropProps
    {
        public static readonly DependencyProperty EnabledForDragProperty = DependencyProperty.RegisterAttached("EnabledForDrag",
            typeof(bool), typeof(DragAndDropProps), new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnEnabledForDragChanged)));
        public static bool GetEnabledForDrag(DependencyObject d) => (bool)d.GetValue(EnabledForDragProperty);
        public static void SetEnabledForDrag(DependencyObject d, bool value) => d.SetValue(EnabledForDragProperty, value);
        private static void OnEnabledForDragChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            FrameworkElement fe = (FrameworkElement)d;
            if ((bool)e.NewValue)
            {
                fe.PreviewMouseDown += Fe_PreviewMouseDown;
                fe.PreviewMouseMove += Fe_PreviewMouseMove;
            }
            else
            {
                fe.PreviewMouseDown -= Fe_PreviewMouseDown;
                fe.PreviewMouseMove -= Fe_PreviewMouseMove;
            }
        }


        public static readonly DependencyProperty DragStartPointProperty = DependencyProperty.RegisterAttached("DragStartPoint",
            typeof(Point?), typeof(DragAndDropProps));
        public static Point? GetDragStartPoint(DependencyObject d) => (Point?)d.GetValue(DragStartPointProperty);
        public static void SetDragStartPoint(DependencyObject d, Point? value) => d.SetValue(DragStartPointProperty, value);


        private static void Fe_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            Debug.WriteLine(e.GetPosition((IInputElement)sender));
            if (e.LeftButton != MouseButtonState.Pressed/* && ((FrameworkElement)sender).IsPressed == true*/) return;

            Point? dragStartPoint = GetDragStartPoint((DependencyObject)sender);
            if (dragStartPoint == null) return;

            var point = e.GetPosition((IInputElement)sender);
            if (dragStartPoint == point) return;

            if (dragStartPoint.HasValue && ((FrameworkElement)sender).DataContext is SharpBase toolBoxData)
            {
                string? type = toolBoxData.GetType().FullName?.Replace("ToolItem", "");
                if (type == null) return;

                DragObject dataObject = new DragObject();
                dataObject.SharpBase = Activator.CreateInstance(Type.GetType(type)) as SharpBase;

                /*
                 * DoDragDrop 方法用于开始一个拖放操作。这个方法需要三个参数，并且是在拖动源（拖动开始的UI元素）上被调用的。其签名如下：
                 * dragSource：拖动源，即触发拖放操作的UI元素，必须是一个继承自 DependencyObject 的类型。
                 * data：需要传输的数据。这通常是一个 DataObject 实例，可以包含多种数据格式，以便目标元素能够识别并处理拖放的数据。
                 * allowedEffects：指定允许的拖放效果，例如 Copy、Move 或 Link，可以通过按位或（|）组合这些值。
 

                在WPF中，实现对图像路径的拖动和接收涉及到拖动源和拖放目标两端的设置。*/
                DragDrop.DoDragDrop((DependencyObject)sender, dataObject, DragDropEffects.Copy);
                e.Handled = true;
                Debug.WriteLine("DragDrop.DoDragDrop");
            }
        }
        private static void Fe_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            SetDragStartPoint((DependencyObject)sender, e.GetPosition((IInputElement)sender));
            //e.Handled = true;
        }

    }
}
